package cn.seapon.talker.adapters;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import cn.seapon.talker.R;
import cn.seapon.talker.infocarrier.Expression;
/**
 * 表情列表适配器
 * @author Asuron
 * Created on 2014年12月31日 下午3:04:48
 */
public class ExpressionGridAdapter extends BaseAdapter {
	
	private static final String TAG = "ExpressionGridAdapter类";
	static int count = 0;
	
	List<Expression> expressions;
	Context context;
	LayoutInflater inflater;

	public ExpressionGridAdapter(Context context,List<Expression> expressions) {
		this.context = context;
		this.expressions = expressions;
		inflater = LayoutInflater.from(context);
	}

	public int getCount() {
		return expressions.size();
	}

	public Object getItem(int position) {
		return expressions.get(position);
	}

	public long getItemId(int position) {
		return position;
	}
	
	/**
	 * Get a View that displays the data at the specified position in the data set
	 * @param convertView The old view to reuse, if possible. Note: You should check that this view is non-null 
	 * 									and of an appropriate type before using. If it is not possible to convert this view to 
	 * 				 					display the correct data, this method can create a new view
	 */
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder = null;
		Expression expression = expressions.get(position);
		if (convertView == null) {
			convertView = inflater.inflate(R.layout.expression_list_item, null);
			holder = new ViewHolder();
			holder.IMAGE_VIEW = (ImageView)convertView.findViewById(R.id.imageview);
			holder.TEXT_VIEW = (TextView)convertView.findViewById(R.id.textview);
			//View.setTag(Onbect)表示给View添加一个格外的数据,可以使用getTag获取该数据 	
			convertView.setTag(holder);
			
		} else {
			//convertView不为null时表示已经设置了额外的数据
			holder = (ViewHolder) convertView.getTag();
		}
		//设置资源  textview是隐藏的
//		Log.d(TAG, "_____"+expression.getDrableId()+" _______"+expression.getCode());
		holder.IMAGE_VIEW.setBackgroundResource(expression.getDrableId());
		holder.TEXT_VIEW.setText(expression.getCode());
//		Log.d(TAG,  convertView.toString()+" _____"+count++);
		return convertView;
	}
	
	private class ViewHolder{
		public ImageView IMAGE_VIEW;
		public TextView TEXT_VIEW;
	}
}